package com.zdy.im.user.dao;


import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixeco.framework.common.data.BasePage;
import com.sixeco.framework.core.db.IterableConverter;
import com.sixeco.framework.core.db.ext.Query;
import com.sixeco.framework.core.mybatis.MyBatisBaseCacheDao;
import com.sixeco.framework.util.PageUtils;
import com.zdy.im.user.dto.request.ImRoomChatMessageRequestDto;
import com.zdy.im.user.dto.response.ImRoomChatMessageResponseDto;
import com.zdy.im.user.entity.ImRoomChatMessage;
import com.zdy.im.user.mapper.ImRoomChatMessageMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.ArrayList;
import java.util.List;




@Service("imRoomChatMessageDao")
public class ImRoomChatMessageDao extends MyBatisBaseCacheDao<ImRoomChatMessageMapper, ImRoomChatMessage> {

    @Autowired
    private ImRoomChatMessageMapper  imRoomChatMessageMapper;

    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ImRoomChatMessage>  iPage =  new Query<ImRoomChatMessage>().getPage(params);

        QueryWrapper<ImRoomChatMessage> query =  new QueryWrapper<>();

        IPage<ImRoomChatMessage> page = this.page(iPage,query );

        return new PageUtils(page);
    }


    @Override
    public void saveData(List<Object> list) {
        List<ImRoomChatMessage> datas = new ArrayList<>();
        list.forEach(e -> datas.add(ImRoomChatMessage.build().clone(e)));
        this.saveBatch(datas, BATCH_SIZE);
        }


    /**
     * 通过分页获取ImRoomChatMessage信息实现查找缓存和数据库的方法
     * @param basePage BasePage,roomId, meesgaeId,
     * @return
     * @author suven
     * @date 2021-05-12 17:26:21
     */
    public List<ImRoomChatMessageResponseDto>  getImRoomChatMessageByRoomIdPage(BasePage basePage){
        List<ImRoomChatMessageResponseDto> resDtoList = new ArrayList<>();
        if(basePage == null){
            return resDtoList;
        }
        //分页对象        PageHelper
        ImRoomChatMessageRequestDto imChatMessageRequestDto = (ImRoomChatMessageRequestDto) basePage.getParamObject();
        if(imChatMessageRequestDto == null){
            return resDtoList;
        }
        Page<ImRoomChatMessage> iPage = new Page<>(basePage.getPageNo(), basePage.getRealPageSize());
        iPage.setSearchCount(false);
        QueryWrapper<ImRoomChatMessage> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("to_id",imChatMessageRequestDto.getToId());
        queryWrapper.le("read_status", 1);//未读和已读聊天记录
        if(imChatMessageRequestDto.getGlobalId() > 0){
            queryWrapper.lt("global_id",imChatMessageRequestDto.getGlobalId());
        }
        iPage.addOrder(OrderItem.desc("global_id"));
        IPage<ImRoomChatMessage> page = this.page(iPage, queryWrapper);
        if(null == page){
            return resDtoList;
        }

        List<ImRoomChatMessage>  list = page.getRecords();
        if(null == list || list.isEmpty()){
            return resDtoList;
        }
        resDtoList = IterableConverter.convertList(list,ImRoomChatMessageResponseDto.class);
        return resDtoList;
    }


}